package srv

import (
	dbModel "back/entity/model"
	"back/entity/nailEntity"
	"back/entity/nailRepo"
	requset "back/router/request"
	"context"
	"github.com/redis/go-redis/v9"
	"gorm.io/gen"
	"utilx/dbx/cache"
)

type MemberCard struct {
	Repo   nailRepo.NailRepo
	bucket cache.RedisBucket
	redis  *redis.Client
}

func NewMemberCard(repo nailRepo.NailRepo, bucket cache.RedisBucket) MemberCard {
	client := bucket.GetClient(1) // 1
	return MemberCard{repo, bucket, client}
}
func (c MemberCard) CacheSearchById(force bool, id int64) (any, error) {
	return cache.GetAop[nailEntity.NailMemberCard](force, c.redis, dbModel.RedisKey(dbModel.TypeMemberCard, id), func() (any, error) {
		return c.SearchById(id)
	})
}
func (c MemberCard) CacheSearchBy(force bool, rec requset.SearchWithPage[nailEntity.NailMemberCard]) (any, error) {
	return cache.GetAop[requset.PageResult[nailEntity.NailMemberCard]](force, c.redis, dbModel.RedisKey(dbModel.TypeMemberCard, rec), func() (any, error) {
		return c.SearchBy(rec)
	})

}

func (c MemberCard) SearchById(id int64) (*nailEntity.NailMemberCard, error) {
	return c.Repo.NailMemberCard.Where(nailRepo.NailMemberCard.ID.Eq(id)).First()
}
func (c MemberCard) SearchBy(rec requset.SearchWithPage[nailEntity.NailMemberCard]) (requset.PageResult[nailEntity.NailMemberCard], error) {
	do := c.Repo.NailMemberCard.Debug()
	do = do.Where(nailRepo.NailMemberCard.MemberName.Like("%" + rec.Data.MemberName + "%"))
	byPage, i, err := do.Order(nailRepo.NailMemberCard.CreatedAt.Desc()).FindByPage(int(rec.PageInfo.PageSize)*int(rec.PageInfo.Page-1), int(rec.PageInfo.PageSize))
	return requset.PageResult[nailEntity.NailMemberCard]{
		Items: byPage,
		Total: i,
	}, err
}
func (c MemberCard) DelById(id int64) (gen.ResultInfo, error) {

	return c.Repo.NailMemberCard.Where(nailRepo.NailMemberCard.ID.Eq(id)).Delete()
}
func (c MemberCard) UpdateById(rec nailEntity.NailMemberCard) error {
	return c.Repo.NailMemberCard.Where(nailRepo.NailMemberCard.ID.Eq(rec.ID)).Save(&rec)
}
func (c MemberCard) UpdateBy(rec requset.UpdateBy[nailEntity.NailMemberCard, nailEntity.NailMemberCard]) error {

	return c.Repo.NailMemberCard.Where(nailRepo.NailMemberCard.ID.Eq(rec.Where.ID)).Save(&rec.Target)
}
func (c MemberCard) Create(rec nailEntity.NailMemberCard) error {
	return c.Repo.NailMemberCard.Create(&rec)
}

// TopUP =======
// 充值
func (c MemberCard) TopUP(rec nailEntity.NailMemberCard) error {
	return nailRepo.Q.Transaction(func(tx *nailRepo.Query) error {
		background := context.Background()
		cardInfo, err := c.SearchById(rec.ID)
		if err != nil {
			return err
		}
		// 充值记录
		tx.NailMemberCardLog.WithContext(background).Create(&nailEntity.NailMemberCardLog{
			MemberID:   int32(cardInfo.ID),
			MemberName: cardInfo.MemberName,
			UpAmount:   rec.UpAmount,
			Balance:    cardInfo.Balance + rec.UpAmount,
			Note:       "",
		})
		_, err = tx.NailMemberCard.WithContext(background).Where(nailRepo.NailMemberCard.ID.Eq(rec.ID)).UpdateColumnSimple(
			nailRepo.NailMemberCard.Balance.Add(cardInfo.UpAmount))
		if err != nil {
			return err
		}
		return nil
	})

}
func (c MemberCard) TopUPX(cardId int64, upAmount int32) error {
	return nailRepo.Q.Transaction(func(tx *nailRepo.Query) error {
		background := context.Background()
		cardInfo, err := c.SearchById(cardId)
		if err != nil {
			return err
		}
		// 充值记录
		tx.NailMemberCardLog.WithContext(background).Create(&nailEntity.NailMemberCardLog{
			MemberID:   int32(cardInfo.ID),
			MemberName: cardInfo.MemberName,
			UpAmount:   upAmount,
			Balance:    cardInfo.Balance + upAmount,
			Note:       "",
		})
		_, err = tx.NailMemberCard.WithContext(background).Where(nailRepo.NailMemberCard.ID.Eq(cardId)).UpdateColumnSimple(
			nailRepo.NailMemberCard.Balance.Add(cardInfo.UpAmount))
		if err != nil {
			return err
		}
		return nil
	})

}
